package com.micro.app.config;

import com.micro.common.constant.Constant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author pan.shun
 * @version 2.0
 * @since 2022/7/13 20:20
 */
@Slf4j
@Configuration
public class MyFeignRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (null != attributes) {
            HttpServletRequest request = attributes.getRequest();
            String token = request.getHeader(Constant.TOKEN_CONFIG.TOKEN_KEY);
            log.info("Feign >> {}", token);
            requestTemplate.header(Constant.TOKEN_CONFIG.TOKEN_KEY, token);
        }
    }
}
